/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _CONSOLEH_
#define _CONSOLEH_

#include "queue.h"
#include "iosdrv.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DEFAULT_NODE {NULL}

typedef struct Node Node;

struct Node {
    SLIST_ENTRY(Node) node;
};

typedef enum _enConsoleResult {
    CE_OK = 0,
    CE_NOT_FOUND = -1,
    CE_PARAM_ERROR = -2,
    CE_RUNTIME_ERROR = -3,
    CE_ACCESS_FORBIDDEN = -4,
    CE_CMD_TOO_LONG = -5
}enConsoleResult;

typedef enConsoleResult(*PCOMMAND_FUN)(int argc, char** argv);

typedef struct _tag_command{
    SLIST_ENTRY(Node) node;
    const char *name;
    PCOMMAND_FUN action;
#ifdef COM_CONSOLE_HELPINFO    
    const char *help;
#endif    
}COMMAND;

#ifdef COM_USE_CONSOLE

    void    ConsoleInit(void);
    void    ConsoleRegCmd(COMMAND* cmd);
    void    ConsoleRun(void);

    #define CONSOLE_REG_CMD(cmds) \
        for (int i = 0; i < sizeof(cmds) / sizeof(COMMAND); i++) { \
                ConsoleRegCmd(&cmds[i]);  \
        }

    #define BGEIN_DEF_CMD(array) \
        static COMMAND array[] = {

    #define END_DEF_CMD(array) \
        };

    #ifdef COM_CONSOLE_HELPINFO
        #define CMD_DEF_ALISA(name, action, info) \
            { DEFAULT_NODE, #name, action, info },

        #define CMD_DEF(action, info)   \
            { DEFAULT_NODE, #action, action, info },
            
    #else
        #define CMD_DEF_ALISA(name, action, info) \
            { DEFAULT_NODE, #name, action },

        #define CMD_DEF(action, info)   \
            { DEFAULT_NODE, #action, action },
    #endif
#else
    #define    ConsoleInit()
    #define    ConsoleRegCmd(cmd)
    #define    ConsoleRun(void)

    #define CONSOLE_REG_CMD(cmds)
    #define BGEIN_DEF_CMD(array) 
    #define END_DEF_CMD(array) 
    #define CMD_DEF_ALISA(name, action, info)
    #define CMD_DEF(action, info)
#endif

#ifdef __cplusplus
}
#endif

#endif /* _CONSOLEH_ */

